import controlP5.*;

class Ball {
  
 int id;
 float x;
 float y;
 float endX;
 float endY;
 float colHue;
 float w ;
 float h ;
 String title;
 PImage img=loadImage("star.jpg");
 film f; 
 Cluster c;
 boolean highlighted;
 int buttonpresslimiter=0;
 boolean buttonPressedDispatch=false;
 float s; // saturation & brightness
 Ball(int id_, float x_, float y_,  String title,film f) {

   id = id_;
   x = x_;
   y = y_;
   endX = x_;
   endY = y_;
   this.title=title;
   this.f=f;
   highlighted = false;
   
 }
 
 void setCluster(Cluster c){
    this.c=c;
 }
  
 void setColor(float hue, int p0,int p1, int l0, int l1){
    colHue = hue;
    // popularity toggled
    if(togglePOP){
    s = (float) f.leng / (l1);
    s=s*250;}
    else {
    s = (float) f.popularity / (p1);
    s=s*250;
    }
    
 }
 float calRADPOP(float m,int x){
   float z =(float) x/88;
   z=z*m;
 return z;
 }
 
 float calRADLENG(float m,int x){
   float z =(float) x/112;
   z=z*m;
 return z;
 }
 
 void display() {
int m = c.calQ();

  
   noStroke();
   
   x = x + (endX - x) * damping;
   y = y + (endY - y) * damping;
  // x=endX;y=endY;

   w=m;
  if(togglePOP) {w = calRADPOP(w,f.popularity);}
  else {w = calRADLENG(w,f.leng);}
  if(highlighted)   { 
    colorMode(HSB,255);
  fill(colHue, 40, 40);
   ellipse(x, y, 250, 250);
   fill(140,162,165);
   textAlign(LEFT);
   textFont(font);
   textSize(16);
//    int year,leng,popularity;
// String title,subject,img;
// boolean awards;
// director dir;
// actor ac,acs;
   text("Title:    "+title, x-100, y-60);
   text("Year:     "+f.year, x-100, y-40);
   text("Length:   "+f.leng, x-100, y-20);
   text("Genre:    "+f.subject, x-100, y);
   text("Director: "+f.dir.name, x-100, y+20);
   text("Actor:    "+f.ac.name, x-100, y+40);
   text("Actress:  "+f.acs.name, x-100, y+60);
   if(f.awards)
   {
     
     image(img,x-16,y-115,32,32);
   }
   drawButton();
   
   
   
 }
   else {h=w;
 colorMode(HSB,255);
  fill(colHue, s, s);
 
   ellipse(x, y, w, h);
      // draw label
   fill(100);
   textAlign(CENTER);
   textFont(font);
   textSize((w/m)*10+7);
   text("" + title, x, y+5);
 }
 

 }
  
 void position(float x_, float y_) {
   endX = x_;
   endY = y_;
 }
 
 boolean  testHighlighted(float x0, float y0){
   buttonPressedDispatch=buttonPressed();
 if( x-w <x0 && x0<x+w && y-w<y0 && y0<y+w ){  
      highlighted=true; 
      text(f.popularity, x, y+20);
     println(f.title+" hightlighted " ); 
      return true;
      }else { 
        highlighted=false;  
      return false;
      }
 }
 
 void drawButton(){


  stroke(1);
  fill(133,228,228);
  rect(x-43, y+80, 90, 30);
  fill(255);
  textAlign(CENTER);
  text("Show links", x, y+100);
      line(x-43,y+80,x+47,y+80);
//    line(x-43,y+110,x+47,y+110);
//    line(x-43,y+80,x-43,y+110);
//    line(x+47,y+80,x+47,y+110);
}
//************ Button pressed 
boolean buttonPressed(){
  if(!highlighted)
  return false;
  if(mousePressed && mouseButton == LEFT&&buttonpresslimiter==0)
  {
  
  if( mouseX  >=  x-43 && mouseX<= x+47 && mouseY >= y+80 && mouseY<= y+110 )
    {  buttonpresslimiter+=1;//dont remove this
      println("button ok");
      return true;
    
    }
    
  }
  if(!mousePressed&&mouseButton==LEFT)
  {
    buttonpresslimiter=0;
  }
  return false;
}

 
}

